package com.daedalus.Inheritanca.Biblioteka;/*
 * Created with IntelliJ IDEA.
 * Author: Kostika Gorica
 * Date: 12-12-03
 * Time: 4.30.MD
 */

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
        Klient[] klientet;
        Liber[] librat;

        int nrKlienteve;
        int nrLibrave;

        int[][] matrica;
        Scanner input = new Scanner(System.in);
        System.out.print("Jepni numrin e Klienteve: ");
        nrKlienteve=input.nextInt();

        System.out.print("Jepni numrin e Librave");
        nrLibrave=input.nextInt();

        klientet = new Klient[nrKlienteve];
        librat = new Liber[nrLibrave];
        matrica = new int[nrKlienteve][nrLibrave];
        input.nextLine();

        for (int i=0; i<klientet.length; i++){
            System.out.print("Emri: ");
            String emri = input.nextLine();
            System.out.print("Mbiemri: ");
            String mbiemri = input.nextLine();
            klientet[i] = new Klient(emri,mbiemri);
        }

        for (int i=0; i<librat.length; i++){
            System.out.print("Titulli: ");
            String titulli = input.nextLine();
            System.out.print("Autori: ");
            String autori = input.nextLine();

            librat[i] = new Liber(titulli, autori);
        }


        for (int i = 0; i < matrica.length; i++){
            for (int j = 0; j< matrica[i].length; j++){
                System.out.printf("Klienti: %s  Libri: %s = ",klientet[i].getEmri(), librat[j].getTitulli());
                matrica[i][j]=input.nextInt();
            }
        }
        //printoj matricen ne menyre estetike per te verifikuar qe te dhenat e futura jane sakte.

        //printoj te gjithe titujt e librave
        System.out.printf("%10s", " ");
        for (int i=0; i< librat.length; i++){
            System.out.printf("%10s",librat[i].getTitulli());
        }
        System.out.println();
        // printoj porosite e klienteve per cdo liber
        for (int i = 0; i < matrica.length; i++){
            //printoj emrin e klentit:
            System.out.printf("%10s",klientet[i].getEmri());
            //printoj porosite e tij per secilin liber
            for (int j = 0; j< matrica[i].length; j++){
                System.out.printf("%10s",matrica[i][j]);
            }
            System.out.println();
        }

        //gjej numrin e porosive per cdo klient
        int[] nrTotalPorosive = new int[nrKlienteve];
        for (int i = 0; i < matrica.length; i++){
            int shumaPorosive = 0;
            for (int j = 0; j< matrica[i].length; j++){
                shumaPorosive+=matrica[i][j];
            }
            nrTotalPorosive[i]=shumaPorosive;
        }
        //i afishoj bukur ne ekran
        for (int i=0; i< klientet.length; i++){
            System.out.println("Klienti: "+klientet[i].getEmri()+"Porosi Totale:"+nrTotalPorosive[i]);
        }

        //gjej indeksin kush ka bere me shume porosi
        int max=0;
        int indexiMax = -1;
        for (int i = 0; i<nrTotalPorosive.length; i++){
             if (nrTotalPorosive[i]>max){
                 max=nrTotalPorosive[i];
                 indexiMax=i;
             }
        }
        // e afishoj klientin me potencial ne ekran
        System.out.println("klienti me me shume porosi: "+klientet[indexiMax].getEmri());


        // tani duhet te rendis klientet sipas nr te librave te porositur (porosi te ndryshme

        // gjej numrin e librave te ndryshem qe ka marre nje klient
        int[] nrLibraveNdryshem = new int[nrKlienteve];
        for (int i = 0; i < matrica.length; i++){
            for (int j = 0; j< matrica[i].length; j++){
                if (matrica[i][j]>0){   //dmth libri eshte porositur te pakten nje here
                    nrLibraveNdryshem[i]++;
                }
            }
        }

        /*
        tani duhet te rendis vektorin  nrLibraveNdryshem ne rend zbrites,
        kujdes !!! per cdo vend te ndruar duhet te ndroj vendet e klienteve ne vektorine klientet
        dhe te kembej rreshtat e klienteve ne matrice
         */

        //rendis vektorin
        for (int i=0; i< nrLibraveNdryshem.length -1 ; i++){
            for (int j=i+1; j< nrLibraveNdryshem.length; j++){
                if (nrLibraveNdryshem[i]<nrLibraveNdryshem[j]){     //nese duhen ndruar vendet
                    // ndroj vendet e nrLibrave
                    int temp = nrLibraveNdryshem[i];
                    nrLibraveNdryshem[i] = nrLibraveNdryshem[j];
                    nrLibraveNdryshem[j] = temp;

                    //ndroj vendet e klienteve
                    Klient klientTemp = klientet[i];
                    klientet[i] = klientet[j];
                    klientet[j] = klientTemp;

                    // nderroj vendet e rreshtave
                    //pra per cdo element te rreshtit
                    for (int k=0; k<matrica[i].length; k++){
                        int tmp = matrica[i][k];
                        matrica[i][k] = matrica[j][k];
                        matrica[j][k] = tmp;
                    }

                }
            }
        }

        //printoj matricen ne menyre estetike per te verifikuar qe te dhenat u renditen

        //printoj te gjithe titujt e librave
        System.out.printf("%10s", " ");
        for (int i=0; i< librat.length; i++){
            System.out.printf("%10s",librat[i].getTitulli());
        }
        System.out.println();
        // printoj porosite e klienteve per cdo liber
        for (int i = 0; i < matrica.length; i++){
            //printoj emrin e klentit:
            System.out.printf("%10s",klientet[i].getEmri());
            //printoj porosite e tij per secilin liber
            for (int j = 0; j< matrica[i].length; j++){
                System.out.printf("%10s",matrica[i][j]);
            }
            System.out.println();
        }
    }

}
